<?php
/* 
 * 家谱，包含一个创建者和户主，
 * 创建者来管理家谱；
 * 家谱从户主开始向上或者向下构建
 */
class M_qin_family extends Cemvc_Db_MysqlDb{
  public $PrimaryKey='id';
  /*CEPHP会在要需要时查询主键，但主动设置主键可以使程序执行更快*/

  public function __construct()
  {
    parent::__construct();
  }

  /* insert a family */
  public function add($array)
  {
    $this->insert(array("name" =>isset($array["name"]) ? $array["name"] : '',
			"region_code" => isset($array["region_code"]) ? $array["region_code"] : '',
			"notes" => isset($array["notes"]) ? $array["notes"] : '',
			"creator" => isset($array["creator"]) ? $array["creator"] : '',
			"open" => isset($array["open"]) ? $array["open"] : '',
			"master" => isset($array["master"]) ? $array["master"] : ''));
    /* delete the old cached info */
    $this->get_families($array["creator"]);
    $key = md5($this->LastQueryString);
    if (function_exists("apc_fetch") && apc_exists($key))
	apc_delete($key);
    
    $family = $this->where("creator=".$array["creator"])->orderBy("id desc")->fetchOne();
    /* return array("resp_id"=>RESP_OK, "resp_str"=>"添加家谱成功。"); */

    /* add to history */
    $history = new M_qin_history();
    $history->add(array("content_type" => CONTENT_TYPE_FAMILY,
			"content_id"   => $family["id"],
			"content"      => base64_encode(serialize($family)),
			"operate_type" => OPERATE_TYPE_ADD,
			"author_id"    => $_SESSION["id"],
			"author_name"  => $_SESSION["realname"]));

    return $family;
  }

  public function update_family($id, $array)
  {
    $res = $this->where(array("id = $id"))->update($array);

    /* delete the old cached info */
    $family = $this->get($id);
    M_Tools::del_cache($this->LastQueryString);
    $this->get_families($family["creator"]);
    M_Tools::del_cache($this->LastQueryString);

    $family = $this->get($id);
    /* add to history */
    $history = new M_qin_history();
    $history->add(array("content_type" => CONTENT_TYPE_FAMILY,
			"content_id"   => $family["id"],
			"content"      => base64_encode(serialize($family)),
			"operate_type" => OPERATE_TYPE_EDIT,
			"author_id"    => $_SESSION["id"],
			"author_name"  => $_SESSION["realname"]));
    return $res;
  }

  public function get($id)
  {
    $sql = "select f.id, f.name, f.notes,f.region_code, f.open, f.creator, u1.realname as creator_name, u2.realname as master_name from family f inner join users u1 on f.creator=u1.id inner join users u2 on f.master = u2.id where f.id=$id";
    $this->mysqlQuery($sql);
    return $this->fetchOne();
  }
  /* get all families created by creator */
  public function get_families($creator)
  {
    $sql = "select f.id, f.name, f.notes,f.region_code, f.master, u2.realname as master_name from family f inner join users u2 on f.master = u2.id where f.creator=$creator";
    $this->mysqlQuery($sql);
    return $this->fetchAll();
  }

  /* find families with region_code and name */
  public function find_families($region, $name)
  {
    $where = array();
    if ($region)
      $where[] = "region_code = $region";
    if ($name)
      $where[] = "name like '%${name}%'";

    if (!$where)
      return null;
    else
      return $this->where($where)->fetchAll();
  }

}